package jlu.zqq.struts.action;

import javax.servlet.http.HttpServletRequest;

import jlu.zqq.dao.RolePermissionsDAO;
import jlu.zqq.po.RolePermissions;

import org.apache.struts2.ServletActionContext;

public class RolePermissionAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8835591484636684297L;
	
	private String role ;
	
	private int useradd=0;
	private int userdel=0;
	private int userupdate=0;
	private int userselect=0;
	
	private int ftadd=0;
	private int ftdel=0;
	private int ftupdate;
	private int ftselect;
	
	private int fradd;
	private int frdel;
	private int frupdate;
	private int frselect;
	
	private int rpadd;
	private int rpdel;
	private int rpupdate;
	private int rpselect;
	
	public String selectRolePermiss(){
		if (!getSessionMap().get("role").equals("a")) {
			rruser = (RolePermissions) getSessionMap().get("rfRolePermis");
			if (rruser.getSelectE() != 1) {
				return "nopermiss";
			}
		}
		RolePermissionsDAO dao = new RolePermissionsDAO();
		RolePermissions rpUser = dao.findByRoleAndTable(role,"user");
		RolePermissions rpFault = dao.findByRoleAndTable(role,"fault");
		RolePermissions rpfRecord = dao.findByRoleAndTable(role,"faultRecord");
		RolePermissions rpRolePermis = dao.findByRoleAndTable(role,"rp");
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("rfault", rpFault);
		request.setAttribute("ruser",rpUser);
		request.setAttribute("rfRecord", rpfRecord);
		request.setAttribute("rfRolePermis", rpRolePermis);
		request.setAttribute("role", role);
		return SUCCESS ;
	}
	public String updateRolePermiss() {
		if (!getSessionMap().get("role").equals("a")) {
			rruser = (RolePermissions) getSessionMap().get("rfRolePermis");
			if(rruser.getSelectE()!=1||rruser.getUpdateE()!=1)
			{
				return "nopermiss";
			}
		}
		RolePermissionsDAO dao = new RolePermissionsDAO();
		RolePermissions userRolePermiss = new RolePermissions(1, role, "user",
				useradd, userdel, userupdate, userselect);
		RolePermissions ftRolePermiss = new RolePermissions(1, role, "fault",
				ftadd, ftdel, ftupdate, ftselect);
		RolePermissions frRolePermiss = new RolePermissions(1, role, "faultRecord",
				fradd, frdel, frupdate, frselect);
		RolePermissions rpRolePermiss = new RolePermissions(1, role, "rp",
				rpadd, rpdel, rpupdate, rpselect);
		try{
			int usertableid = dao.findID(role, "user");
			int faulttableid = dao.findID(role, "fault");
			int faultRecordtableid = dao.findID(role, "faultRecord");
			int rptableid = dao.findID(role, "rp");
			
			if(usertableid==0){
				//dao.save(userRolePermiss);
			}else{
				userRolePermiss.setId(usertableid);
				dao.update(userRolePermiss);
			}
			if(faulttableid==0){
				//dao.save(ftRolePermiss);
			}else{
				ftRolePermiss.setId(faulttableid);
				dao.update(ftRolePermiss);
			}
			if(faultRecordtableid==0){
				//dao.save(frRolePermiss);
			}else{
				frRolePermiss.setId(faultRecordtableid);
				dao.update(frRolePermiss);
			}
			if(rptableid==0){
				//dao.save(rpRolePermiss);
			}else{
				rpRolePermiss.setId(rptableid);
				dao.update(rpRolePermiss);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public int getUseradd() {
		return useradd;
	}
	public void setUseradd(int useradd) {
		this.useradd = useradd;
	}
	public int getUserdel() {
		return userdel;
	}
	public void setUserdel(int userdel) {
		this.userdel = userdel;
	}
	public int getUserupdate() {
		return userupdate;
	}
	public void setUserupdate(int userupdate) {
		this.userupdate = userupdate;
	}
	public int getUserselect() {
		return userselect;
	}
	public void setUserselect(int userselect) {
		this.userselect = userselect;
	}
	public int getFtadd() {
		return ftadd;
	}
	public void setFtadd(int ftadd) {
		this.ftadd = ftadd;
	}
	public int getFtdel() {
		return ftdel;
	}
	public void setFtdel(int ftdel) {
		this.ftdel = ftdel;
	}
	public int getFtupdate() {
		return ftupdate;
	}
	public void setFtupdate(int ftupdate) {
		this.ftupdate = ftupdate;
	}
	public int getFtselect() {
		return ftselect;
	}
	public void setFtselect(int ftselect) {
		this.ftselect = ftselect;
	}
	public int getFradd() {
		return fradd;
	}
	public void setFradd(int fradd) {
		this.fradd = fradd;
	}
	public int getFrdel() {
		return frdel;
	}
	public void setFrdel(int frdel) {
		this.frdel = frdel;
	}
	public int getFrupdate() {
		return frupdate;
	}
	public void setFrupdate(int frupdate) {
		this.frupdate = frupdate;
	}
	public int getFrselect() {
		return frselect;
	}
	public void setFrselect(int frselect) {
		this.frselect = frselect;
	}
	public int getRpadd() {
		return rpadd;
	}
	public void setRpadd(int rpadd) {
		this.rpadd = rpadd;
	}
	public int getRpdel() {
		return rpdel;
	}
	public void setRpdel(int rpdel) {
		this.rpdel = rpdel;
	}
	public int getRpupdate() {
		return rpupdate;
	}
	public void setRpupdate(int rpupdate) {
		this.rpupdate = rpupdate;
	}
	public int getRpselect() {
		return rpselect;
	}
	public void setRpselect(int rpselect) {
		this.rpselect = rpselect;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}
	
}
